﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Efficient
{
    public static class EnumHelper
    {
        private static Dictionary<Type, IDictionary<int, string>> enumMapCache = new Dictionary<Type, IDictionary<int, string>>();

        public static IDictionary<int, string> GetEnumItemMap<T>()
        {
            Type clazz = typeof(T);
            return GetEnumItemMap(clazz);
        }

        public static IDictionary<int, string> GetEnumItemMap(Type clazz)
        {
            if (enumMapCache.ContainsKey(clazz))
            {
                return enumMapCache[clazz];
            }
            Dictionary<int, string> result = new Dictionary<int, string>();
            if (clazz.IsEnum)
            {
                Array values = Enum.GetValues(clazz);
                foreach (int val in values)
                {
                   string name = Enum.GetName(clazz, val);
                   result.Add(val, name);
                }
                enumMapCache[clazz] = result;
            }
            return result;
        }
    }
}
